﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using log4net;
namespace WebServiceSmart
{
    public static class ZhiFuSignature
    {
        /// <summary>
        /// 加密的java包文件地址（绝对路径）
        /// </summary>
        const string cmdPath = "D:\\chuanhuafpx\\javaRSA.jar";
        const string cmdName = "cmd.exe";
        const string rsaurl = "http://localhost:8999/blog/Test";

        /// <summary>
        /// 参数加密 cmd
        /// </summary>
        /// <param name="fpxPath">证书地址</param>
        /// <param name="fpxPassword">证书密码</param>
        /// <param name="paras">请求参数</param>
        /// <returns>签名</returns>
        [Obsolete]
        public static string GetSignature(string fpxPath, string fpxPassword, Dictionary<string, object> paras, string a)
        {
            ILog logJieKou = LogManager.GetLogger("LoggerZhiFu");

            JObject job = new JObject();
            foreach (var item in paras)
            {
                job.Add(item.Key, item.Value.ToString());
            }
            var sig = JsonConvert.SerializeObject(job);
            sig = sig.Replace("\"", "\\\"");
            //fpxPath, fpxPassword, sig

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = cmdName;
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序

            logJieKou.Info("-签名生成开始");
            p.StandardInput.WriteLine(string.Format("java -jar {0} {1} {2} {3}&exit", cmdPath, fpxPath, fpxPassword, sig));

            p.StandardInput.AutoFlush = true;

            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();

            logJieKou.Info("-签名生成完成：" + output);
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

            var indexStr = "签名：";
            var index = output.LastIndexOf(indexStr);
            if (index != -1)
            {
                index += indexStr.Length;
                sig = output.Substring(index).Replace("\r\n", "");
                logJieKou.Info("-签名信息" + sig);
                return sig;
            }
            logJieKou.Info("-签名异常" + output);
            indexStr = "异常：";
            index = output.LastIndexOf(indexStr);
            if (index != -1)
            {
                index += indexStr.Length;
                sig = output.Substring(index).Replace("\r\n", "");
                throw new Exception(sig);
            }
            return string.Empty;

        }


        /// <summary>
        /// 参数加密
        /// </summary>
        /// <param name="fpxPath">证书地址</param>
        /// <param name="fpxPassword">证书密码</param>
        /// <param name="paras">请求参数</param>
        /// <returns>签名</returns>
        public static string GetSignature(string fpxPath, string fpxPassword, Dictionary<string, object> paras)
        {
            ILog logJieKou = LogManager.GetLogger("LoggerZhiFu");

            JObject job = new JObject();
            foreach (var item in paras)
            {
                job.Add(item.Key, item.Value.ToString());
            }
            var sig = JsonConvert.SerializeObject(job);
            //sig = sig.Replace("\"", "\\\"");
            var result = SmsSendInterface.postJson(rsaurl, Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                pfxFileName = fpxPath,
                keyPassword = fpxPassword,
                originalMessage = sig
            }));
            if (!string.IsNullOrWhiteSpace(result))
            {
                return result;
            }
            return result;
        }
        public static string chuanhuaurl = System.Configuration.ConfigurationManager.AppSettings["ChuanHuaXinYongFuUrl"];
        private static string _enterprise_code = System.Configuration.ConfigurationManager.AppSettings["enterprise_code"];
        private static string _channel_code = System.Configuration.ConfigurationManager.AppSettings["channel_code"];
        private static string _access_no = System.Configuration.ConfigurationManager.AppSettings["access_no"];
        private static string _appid = System.Configuration.ConfigurationManager.AppSettings["appid"];
        private static string _zhengshupath = System.Configuration.ConfigurationManager.AppSettings["zhengshupath"];
        private static string _zhengshupwd = System.Configuration.ConfigurationManager.AppSettings["zhengshupwd"];
        public static string GetTestSignature(string serviceid, string trm_type, string ip, Dictionary<string, object> paras)
        {
            paras.Add("enterprise_code", _enterprise_code); //6688421433017541
            paras.Add("channel_code", _channel_code);
            paras.Add("access_no", _access_no);
            paras.Add("appid", _appid);//测试 //3657278

            paras.Add("tf_timestamp", DateTime.Now.ToString("yyyyMMddHHmmss"));
            paras.Add("service_id", serviceid);
            paras.Add("sign_type", "RSA");
            paras.Add("version", "1.0");
            paras.Add("trm_type", trm_type);
            paras.Add("client_ip", ip);
            return GetSignature(_zhengshupath, _zhengshupwd, paras);
        }

    }
}
